package service.depr;

import java.util.Collection;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import process.CaseProcess;
import service.AbstractService;
import entity.Case;
import entity.Catalog;

@Path("/")
public class CaseService extends AbstractService {
	
	protected CaseProcess caseProcess=new CaseProcess();
	
	@Path(ACTION_CASESTREE+"/{id:\\d+}")//"/bpccasetree/{id:\\d+}"
	@GET
	@Produces("application/json")
	public  Collection<Catalog> getCaseTree(@PathParam("id") long id){
		List<Catalog> Catalogs=null;
		try{
			Catalogs=caseProcess.getEntities(Catalog.class.getName());
			System.out.println("id "+id);
			for(Catalog catalog:Catalogs){
				caseProcess.getCasesByCatalog(catalog.getId());				
			}
			
			//System.out.println("Cases "+Cases.size());
		}
		catch(Exception ex){
			this.LogException(ex);
		}
		return Catalogs;
	}
	@Path(ACTION_SAVECASE)
	@POST
	@Consumes("application/json")
	public  Response  saveCase(Case entity){
		//System.out.println("input name "+entity.getName());
		try{
			System.out.println("input name "+entity.getName());
			caseProcess.saveEntity(entity);
		}
		catch(Exception ex){
			this.LogException(ex);
			return Response.serverError().build();
		}
		Response resp= Response.ok().build();

		return resp;
	}
	@Path(ACTION_CASES+"/{id:\\d+}")
	@GET
	@Produces("application/json")
	public  Collection<Case> getCases(@PathParam("id") long id){
		List<Case> Cases=null;
		try{
			Cases=caseProcess.getEntities(Case.class.getName());
			System.out.println("get Cases "+Cases.size());
		}
		catch(Exception ex){
			this.LogException(ex);
		}
		return Cases;
	}
	
	@Path(ACTION_CASE+"/{id:\\d+}")
	@GET
	@Produces("application/json")
	public  Case getCase(@PathParam("id") long id){
		Case Case=null;
		try{
			//org.datanucleus.api.jpa.PersistenceProviderImpl p=null;
			Case=caseProcess.getEntity(Case.class, id);
			System.out.println("Case "+Case+"  "+id);
		}
		catch(Exception ex){
			this.LogException(ex);
		}
		return Case;
	}
	@Path(ACTION_REMOVECASE+"/{id:\\d+}")
	@POST
	@Consumes("application/json")
	public  Response  removeCase(@PathParam("id") long id){
		try{
			Case entity=new Case();
			entity.setId(id);
			System.out.println("remove id "+entity.getId());
			caseProcess.removeEntity(entity);
		}
		catch(Exception ex){
			this.LogException(ex);
			return Response.serverError().build();
		}
		Response resp= Response.ok().build();

		return resp;
	}
}
